<template>
  <div>
    <form action="/">
      <van-search
        v-model="input"
        show-action
        placeholder="请输入搜索关键词"
        @search="onSearch"
        @mouseleave="onCancel"
      />
    </form>
  </div>
  <div>
    <van-index-bar v-for="(value, index) of list" :key="index">
      <van-index-anchor index="1">{{ value.letter }}</van-index-anchor>
      <van-cell
        :title="city"
        v-for="(city, index) of value.list"
        :key="index"
        @click="setCityName(city)"
      />
    </van-index-bar>
  </div>
</template>
<script>
import axios from "axios";
export default {
  data() {
    return {
      input: "",
      list: [],
      list1: [],
    };
  },
  created() {
    this.load();
  },
  computed: {},
  methods: {
    onSearch() {
      this.list = this.list1.filter((text) => {
        return text.letter.match(this.input);
      });

      console.log(this.list);
    },
    onCancel() {
      this.load();
    },
    setCityName(city) {
      this.$router.push({ path: "/home", query: { city: city } });
    },
    load() {
      if (!this.input) {
        axios
          .get(
            "https://mock.mengxuegu.com/mock/6142955121805563ee103334/example/cityList"
          )
          .then((res) => {
            /*   console.log(res.data.data.cityList); */
            this.list = res.data.data.cityList;
            this.list1 = res.data.data.cityList;
          });
      }
    },
  },
};
</script>
<style lang="scss" scoped>
</style>
